﻿using GrayParrot.Configuration;
using GrayParrot.Core;
using GrayParrot.ServiceBus.Configuration;
using Microsoft.ServiceBus.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GrayParrot.ServiceBus
{
    public enum ServiceBusReaderActions
    {
        Receive = 0,
        Peek
    }

    public class ServiceBusHelper : IDisposable
    {
        SubscriptionClient m_client = null;

        public ServiceBusHelper()
        {

        }

        public ReturnValue<BrokeredMessage> ReadMessage(string subscription, ServiceBusReaderActions action)
        {
            ReturnValue<BrokeredMessage> retValue = new ReturnValue<BrokeredMessage>();
            retValue.HasValue = false;
            retValue.Value = null;

            string Endpoint = string.Empty;
            string Topic = string.Empty;
            string Subscription = string.Empty;
            bool ReadDL = false;

            try
            {
                ConfigurationManagerHelper pp = new ConfigurationManagerHelper();
                Subscription m_subs = pp.GetSubscription(subscription);

                //
                Endpoint = m_subs.ConnectionString;
                Topic = m_subs.Topic;
                Subscription = subscription;
                ReadDL = m_subs.ReadDL;

                if (!ReadDL)
                {
                    // se leggo dalla coda principale...
                    m_client = SubscriptionClient.CreateFromConnectionString(Endpoint, Topic, Subscription);
                }
                else
                {
                    // se leggo dalla dead letter...
                    // https://blogs.blackmarble.co.uk/blogs/sspencer/default.aspx?PageIndex=2&page=2
                    m_client = SubscriptionClient.CreateFromConnectionString(Endpoint, Topic, Subscription + "/$DeadLetterQueue");
                }

                if (m_client != null)
                {
                    switch (action)
                    {
                        case ServiceBusReaderActions.Receive:
                            retValue.Value = m_client.Receive();
                            if (retValue.Value != null)
                            {
                                retValue.Value.Complete();
                            }
                            break;
                        case ServiceBusReaderActions.Peek:
                            retValue.Value = m_client.Peek();
                            break;
                        default:
                            break;
                    }

                    retValue.HasValue = true;
                }
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            //
            return retValue;
        }

        public void Dispose()
        {
            if (m_client != null)
            {
                m_client.Close();
                m_client = null;
            }

            GC.SuppressFinalize(this);
        }
    }
}
